﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Controls.Ribbon;
using Tanmia.Core;
using System.Threading;
using Tanmia.Controls;

namespace Tanmia.Windows.InvoiceScenario
{
    /// <summary>
    /// Interaction logic for AddInvoice.xaml
    /// </summary>
    public partial class InvoiceEdit : RibbonWindow
    {
        InvoiceEditViewModel InvoiceVM = new InvoiceEditViewModel();

        Repository DefalutRepository;

        public InvoiceEdit()
        {
            InitializeComponent();

            DataContext = InvoiceVM;
        }

        public InvoiceEdit(Repository defalutRep)
        {
            InitializeComponent();

            DataContext = InvoiceVM;

            DefalutRepository = defalutRep;
        }

        private void ShowValidationTool(string error)
        {
            vatError.Label = error;
            vatError.Visibility = Visibility.Visible;
        }

        private bool Save()
        {
            InvoiceVM.Entity.Validate();

            if (!InvoiceVM.Entity.IsValid())
            {
                ShowValidationTool(InvoiceVM.Error);
                return false;
            }
            else
            {
                try
                {
                    InvoiceVM.Save();
                    InvoiceVM.Post();

                    if (vatError.Visibility == Visibility.Visible)
                        vatError.Visibility = Visibility.Collapsed;

                    return true;
                }
                catch (Exception ex)
                {
                    ShowValidationTool(ex.Message);

                    return false;
                }
            }
        }


        private void RibbonWindow_Loaded(object sender, RoutedEventArgs e)
        {
            cmbRecord.IsEnabled = false;

            cmbPerson.SelectedIndex = 0;
            dtpCreateDate.SelectedDate = DateTime.Now;
            cmbUser.SelectedIndex = User.CurrentUser.UserID - 1;
            cmbRepository.SelectedItem = DefalutRepository;
            cmbInvoiceType.SelectedIndex = 0;
            cmbPaymentMethod.SelectedIndex = 0;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            Save();
        }

        private void btnSaveAndNew_Click(object sender, RoutedEventArgs e)
        {
            if (Save())
                btnClear_Click(sender, e);
        }

        private void btnSaveAndPayment_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnSaveAndPrint_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnPerviewPrint_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnSetupPrint_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            InvoiceVM.Entity = new Invoice();
            cmbPerson.SelectedIndex = 0;
            dtpCreateDate.SelectedDate = DateTime.Now;
            cmbUser.SelectedIndex = User.CurrentUser.UserID - 1;
            cmbRepository.SelectedItem = DefalutRepository;
            cmbInvoiceType.SelectedIndex = 0;
            cmbPaymentMethod.SelectedIndex = 0;
        }

        private void btnSaveAndClose_Click(object sender, RoutedEventArgs e)
        {
            if (Save())
                Close();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void btnLast_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnNext_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnPrevious_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnFirst_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnSelectAllRow_Click(object sender, RoutedEventArgs e)
        {

        }

        private void tgbShowBorders_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnAddRow_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnInsertAbove_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnInsertBelow_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnDeleteRow_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnDeleteGrid_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnOptions_Click(object sender, RoutedEventArgs e)
        {

        }


        private void ItemsDataGrid_GotFocus(object sender, RoutedEventArgs e)
        {
            tabGridTools.Visibility = Visibility.Visible;
            tabTabel.IsSelected = true;
        }

        private void ItemsDataGrid_LostFocus(object sender, RoutedEventArgs e)
        {
            tabGridTools.Visibility = Visibility.Hidden;

            BindingExpression b = txtTotal.GetBindingExpression(TextBox.TextProperty);
            b.UpdateTarget();

            b = txtFinal.GetBindingExpression(TextBox.TextProperty);
            b.UpdateTarget();
        }

        private void Label_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            object o = DataContext;
           
        }

        private void btnAddDI_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnAddPerson_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnAddRepository_Click(object sender, RoutedEventArgs e)
        {

        }

        private void cmbInvoiceType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            InvoiceVM.UpdatePersons(cmbInvoiceType.SelectedIndex);
            if (cmbPerson != null)
                cmbPerson.SelectedIndex = 0;
        }

        private void ItemsDataGrid_InitializingNewItem(object sender, InitializingNewItemEventArgs e)
        {
            
        }
    }
}
